import store from './store';
import hotkey from '~hotkey';
import { initApp } from './init';
import osUtils from '~common/help/os';
import { runService } from './service';
import { proxyConsole } from './console';
import { switchWindow, createTray } from './window';
import { app, globalShortcut, Menu } from 'electron';
import { IpcRendererEventEnum } from '../common/enum';

// https://www.electronforge.io/config/makers/squirrel.windows
if (require('electron-squirrel-startup')) app.quit();

const isProd = store.state.isProd;
const isWindows = store.state.isWindows;

/**
 * 程序初始化
 */
(async function initProgram() {
    proxyConsole();

    // 初始化用户数据配置
    await initApp();

    app.on('window-all-closed', () => {
        app.quit();
    });

    // 区分 Mac 程序坞退出
    app.on('before-quit', () => {
        store.state.forceQuitWindow = true;
    });

    app.on('activate', () => {
        if (isWindows) {
            // ...
        } else {
            if (isProd) {
                // ...
            } else {
                if (store.state.mainWindow) {
                    store.state.mainWindow.show();
                } else {
                    store.state.lastActiveWindow?.show();
                }
            }
        }
    });

    app.whenReady()
        .then(() => {
            registerGlobalShortcut();

            createTray();
            createMainWindow();

            runService();

            // ...
        })
        .catch(e => {
            console.error(e);
        });
})();

/**
 * 创建主窗口
 */
function createMainWindow(): void {
    // 隐藏菜单栏
    Menu.setApplicationMenu(null);

    if (isProd) {
        loadMainWindow();
    } else {
        const firstProject = osUtils.getArgvValue('firstProject');
        if (firstProject && firstProject !== 'copid') {
            const win = switchWindow(firstProject);
            win.webContents.openDevTools();
        } else {
            loadMainWindow();
            store.state.mainWindow?.webContents.openDevTools();
        }
    }
}

/**
 * 加载主窗口
 */
function loadMainWindow(): void {
    const mainWindow = switchWindow('copid');
    store.setMainWindow(mainWindow);
    store.setLastActiveWindow(mainWindow);
    store.setCurrentActiveWindow(mainWindow);
}

/**
 * 注册全局快捷键
 */
function registerGlobalShortcut() {
    // 显示窗口
    globalShortcut.register('F1', () => {
        if (store.state.mainWindow?.isFocused()) {
            // ...
        } else {
            if (isWindows) {
                store.savePrevActiveWindow();
            }
            store.state.mainWindow?.center();
            store.state.mainWindow?.show();
            store.state.mainWindow?.webContents.send(IpcRendererEventEnum.SHOW_WINDOWS);
        }
    });

    // 显示 Nota 窗口
    globalShortcut.register('Ctrl+Alt+N', () => {
        switchWindow('nota');
    });

    // 剪贴板队列出列
    globalShortcut.register('Ctrl+Shift+V', () => {
        if (store.state.copidRecord.clipboardDataQueue.length) {
            const data = store.state.copidRecord.clipboardDataQueue.shift();
            if (data?.content) hotkey.typeString(data.content);
        }
    });

    // 剪贴板清除队列记录
    globalShortcut.register('Ctrl+Alt+V', () => {
        store.state.copidRecord.clipboardDataQueue = [];
    });
}
